package org.csu.mypetstore.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.csu.mypetstore.domain.*;
import org.csu.mypetstore.service.CatalogService;
import org.csu.mypetstore.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
@RequestMapping("/catalog")
public class CatalogController {
    @Autowired
    private CatalogService catalogService;

    @Autowired
    private LogService logService;

    @GetMapping("/main")
    public String viewMain(HttpSession session,HttpServletRequest request){
        return "catalog/main";
    }

    //主界面进入大类
    @GetMapping("/category")
    public String viewCategory(String categoryId, Model model){
        if(categoryId!=null){
            Category category= catalogService.getCategory(categoryId);
            List<Product> productList= catalogService.getProductListByCategory(categoryId);
            model.addAttribute("category",category);
            model.addAttribute("productList",productList);
            return "catalog/category";
        }
        return "catalog/main";
    }

    //从大类进入品种
    @GetMapping("/product")
    public String viewProduct(String productId, Model model){
        if(productId!=null){
            Product product = catalogService.getProduct(productId);
            List<Item> itemList = catalogService.getItemListByProduct(productId);
            model.addAttribute("product",product);
            model.addAttribute("itemList",itemList);
            return "catalog/product";
        }
        return "catalog/category";
    }

    //从品种进入具体
    @GetMapping("/item")
    public String viewItem(String itemId, HttpSession session,Model model){
        if(itemId!=null){
            Item item = catalogService.getItem(itemId);
            Product product = item.getProduct();
            String img = product.getDescription().toString();
            String[] str=img.split("\"");
            String imgsrc = str[1];
            System.out.println(imgsrc);
            String[] str1=img.split(">");
            String des = str1[1];
            System.out.println(des);
            model.addAttribute("imgsrc",imgsrc);
            session.setAttribute("imgsrc",imgsrc);
            model.addAttribute("description",des);
            session.setAttribute("description",des);
            model.addAttribute("item",item);
            session.setAttribute("item",item);
            model.addAttribute("product",product);
            session.setAttribute("product",product);
            return "catalog/item";
        }
        return "catalog/product";
    }

    @GetMapping("/completeSearch")
    public void searchProducts(HttpServletResponse resp,String keyword, Model model) throws IOException {
        System.out.println(keyword);
        List<Product> productList=catalogService.searchProductList(keyword);
        resp.setContentType("text/json");
        PrintWriter out=resp.getWriter();

        JSONArray searchArray=new JSONArray();
        for(int i=0;i<productList.size();i++){
            //产品名字
            JSONObject searchObject=new JSONObject();
            searchObject.put("name",productList.get(i).getName());
            //添加到数组中
            searchArray.add(searchObject);
        }
        //转换成JSON字符串
        String str=searchArray.toJSONString();
        out.print(str);
    }

    @PostMapping("/searchProduct")
    public void searchProduct(HttpServletResponse resp,HttpSession session, Model model,@RequestParam("keyword") String keyword) throws IOException {

        System.out.println(keyword);
        List<Product> productList = catalogService.searchProductList(keyword);

        //保存数据
        session.setAttribute("keyword", keyword);
        session.setAttribute("productList", productList);

        resp.setContentType("application/json");
        resp.setCharacterEncoding("UTF-8");
        PrintWriter out = resp.getWriter();
        Result result=new Result();

        result.setCode(11);
        result.setMsg("/catalog/searchProduct");
        String str = JSON.toJSONString(result);
        out.print(str);
        out.flush();
        out.close();

    }

    @GetMapping("/searchProduct")
    public String viewSearchProduct(){
        return "catalog/SearchProduct";
    }

    @PostMapping("showInformation")
    public void showInformation(HttpServletResponse resp,HttpSession session,@RequestParam("categoryId") String categoryId) throws IOException{
        resp.setContentType("application/json");//以json形式输出
        PrintWriter out=resp.getWriter();
        Result result1=new Result();
        List<Product> productList = catalogService.getProductListByCategory(categoryId);
        String productId=String.format("%-12s","Product ID");
        String name=String.format("%-26s","Name");
        String itemId=String.format("%-12s","Item Id");
        String response = itemId+"   "+productId+"   "+name+"\n";
        int i = 0;
        while(i < productList.size()){
            Product product = productList.get(i);
            List<Item> itemList = catalogService.getItemListByProduct(product.getProductId());
            for(int j=0;j<itemList.size();j++){
                Item item=itemList.get(j);
                productId=String.format("%-12s", product.getProductId());
                name=String.format("%-26s",product.getName());
                itemId=String.format("%-10s",item.getItemId());
                response += itemId+"   "+productId+"   "+name+"\n";;
            }
            i++;
        }
        result1.setCode(8);
        result1.setMsg(response);
        String str = JSON.toJSONString(result1);
        out.print(str);
        out.flush();
        out.close();
    }
}
